// subcontract/Mine/withdrawal/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    aliyunServrBaseURL: getApp().config.aliyunServrBaseURL,//阿里云公共地址
    tabs: ['提现到银行卡', '提现到支付宝'],
    activeIndex: 0,//选择的下标
    zIndex: 0,
    getBankCardList: [],
    moneyInfo: {},
    total: '0.00',
    value: ''
  },
  async applyWithdraw() {
    let { getBankCardList, activeIndex, zIndex, value, total, moneyInfo } = this.data
    let object = {
      type: activeIndex,
      bank_id: getBankCardList[zIndex].id,
      apply_amount: value,
      left_amount: total,
      poundage_amount: (value - total),
      poundage_ratio: moneyInfo.fee
    }
    const { code, msg } = await getApp().api.applyWithdraw(object)
    if (code == 1) {
      getApp().showToast(msg)
      setTimeout(() => {
        wx.navigateBack()
      }, 2000)
    }
  },
  clear() {
    this.setData({
      value: ''
    })
  },
  all() {
    this.setData({
      value: this.data.moneyInfo.can_apply
    })
    let value = this.data.value
    this.setData({
      total: (value * (((100 - (this.data.moneyInfo.fee * 1)) / 100))).toFixed(2)
    })
  },
  onChangeInput(event) {
    console.log(event);
    let value = event.detail.value
    this.setData({
      value: value,
      total: (value * (((100 - (this.data.moneyInfo.fee * 1)) / 100))).toFixed(2)
    })
  },
  /**我的收入 */
  async userMymoney() {
    const { code, data } = await getApp().api.userMymoney()
    if (code == 1) {
      this.setData({
        moneyInfo: data
      })
    }
  },
  onChange(event) {
    let index = event.detail.index
    this.setData({
      zIndex: 0,
      activeIndex: index,
    })
    if (index == 0) {
      this.getBankCard()
    } else {
      this.getAlipayInfo()
    }
  },
  async getAlipayInfo() {
    let object = {
      agent_id: getApp().userinfo.agent_id,
    }
    const { code, data } = await getApp().api.getAlipayInfo(object)
    if (code == 1) {
      this.setData({
        getBankCardList: [data]
      })
    }
  },
  async getBankCard() {
    let object = {
      agent_id: getApp().userinfo.agent_id,
    }
    const { code, data } = await getApp().api.getBankCard(object)
    if (code == 1) {
      this.setData({
        getBankCardList: data
      })
    }
  },
  navigateTo(event) {
    let url = event.currentTarget.dataset.url
    wx.navigateTo({
      url,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getBankCard()
    this.userMymoney()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})